
<link rel="stylesheet" th:href="@{/ui/css/amazeui.chosen.css}" />
<script th:src="@{/ui/js/amazeui.chosen.js}"></script>
<div class="row-content am-cf">
	<div class="row">
		<div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
			<div class="widget am-cf">
				<div class="widget-body am-fr">
					<form method="post" id="sxRuleForm" >
						<input type="hidden" name="agentId" th:value="${agentId}"/>
						<input type="hidden" name="kydProductId" th:value="${kydProdcut != null?kydProdcut.id:''}"/>
						<input type="hidden" name="yndProductId" th:value="${yndProduct != null?yndProduct.id:''}"/>
						<input type="hidden" name="settingsId" th:value="${settingsId}"/>
						<input type="hidden" name="ynddebxId" th:value="${ynddebxId}"/>
						<input type="hidden" name="ynddebjId" th:value="${ynddebjId}"/>
						<input type="hidden" name="yndayfxId" th:value="${yndayfxId}"/>
						<input type="hidden" name="ynddqbxId" th:value="${ynddqbxId}"/>
						<input type="hidden" name="kyddebxId" th:value="${kyddebxId}"/>
						<input type="hidden" name="kyddebjId" th:value="${kyddebjId}"/>
						<input type="hidden" name="kydayfxId" th:value="${kydayfxId}"/>
						<input type="hidden" name="kyddqbxId" th:value="${kyddqbxId}"/>
						<table class="am-table am-table-bordered">
							<tbody>
								<tr>
									<td colspan="6" class="am-primary">产品配置：</td>
								</tr>
								<tr>
									<td colspan="6" >优农贷</td>
								</tr>
								<tr>
									<td >状态：</td>
									<td>
										<select data-am-selected="{searchBox: 1}"  name="yndStatus"  id="yndStatus">
											<option th:each="status : ${switchEnum}"  th:value="${status.name()}" th:text="${status.getKgdesc()}" 
											th:selected="${status.name() == (yndProduct != null?yndProduct.status:'no')}" ></option>
										</select> 
									</td>
									<td >最短期限：</td>
									<td >
										<select data-am-selected="{searchBox: 1}"  name="yndMinMonths" id="yndMinMonths">
											<span th:each="min : ${NumEnum}" >
												<span th:if="${min.getNum()} != 0">
													<option th:value="${min.getNum()}" th:text="${min.getNum()} + '个月'" 
													th:selected="${min.getNum() == (yndProduct != null?yndProduct.min_months:1)}" ></option>
												</span>
											</span>
										</select>
									</td>
									<td>提前还款最短时间：</td>
									<td>
										<select data-am-selected="{searchBox: 1}" name="yndAdvanceMonth" id="yndAdvanceMonth" >
											<option th:each="min : ${NumEnum}" th:value="${min.getNum()}" th:text="${min.getNum()} + '个月'" 
											th:selected="${min.getNum() == (yndProduct != null?yndProduct.advance_month:0)}"></option>
										</select> 
									</td>
								</tr>
								<tr>
									<td rowspan="2">还款方式&amp;利率：</td>
									<td ><input type="checkbox" value="debx" th:checked="${ ynddebx != null}" id="ynddebxCheck" name="ynddebxCheck"/>等额本息</td>
									<td ><input type="checkbox" value="dqbx" th:checked="${ ynddqbx != null}" id="ynddqbxCheck" name="ynddqbxCheck"/>到期还本付息</td>
									<td ><input type="checkbox" value="ayfx" th:checked="${ yndayfx != null}" id="yndayfxCheck" name="yndayfxCheck"/>按月付息到期还本</td>
									<td  colspan="2"><input  type="checkbox" value="debj" th:checked="${ ynddebj != null}" id="ynddebjCheck" name="ynddebjCheck"/>等本等息</td>
								</tr>
								<tr>
									<td>日利率<input type="number" min="0" th:value="${ ynddebx !=null ? ynddebx :0 }"  id="ynddebxRate" name="ynddebxRate"/>%</td>
									<td>月利率<input type="number" min="0"  th:value="${ ynddqbx !=null ? ynddqbx :0}" id="ynddqbxRate" name="ynddqbxRate"/>%</td>
									<td>日利率<input type="number" min="0" th:value="${ yndayfx !=null ? yndayfx :0}" id="yndayfxRate" name="yndayfxRate"/>%</td>
									<td colspan="4">日利率<input  type="number" min="0"  th:value="${ ynddebj !=null ? ynddebj :0}" id="ynddebjRate" name="ynddebjRate"/>%</td>
								</tr>
								<tr>
									<td >放款合伙人确认 状态：</td>
									<td>
										<select data-am-selected="{searchBox: 1}"  name="yndEnableConfirm" id="yndEnableConfirm" onChange="yndChangeEnableConfirm()">
											<option th:each="status : ${switchEnum}"  th:value="${status.getCode()}" th:text="${status.getKgdesc()}" 
											th:selected="${status.getCode() == ( yndEnableConfirm != null ? yndEnableConfirm :0)}"></option>
										</select>
									</td>
									<td>确认角色：</td>
									<td colspan="3">
										<div id="yndCheckBox">
											<input type="checkbox" value="10"  th:checked="${yndcunCheck != null}" id="yndcunCheck" name="yndcunCheck"/>市场合伙人  
											<input type="checkbox" value="11"  th:checked="${yndzhenCheck != null}" id="yndzhenCheck" name="yndzhenCheck"/>二级运营合伙人 		       
											<input type="checkbox" value="12"  th:checked="${yndxianCheck != null}" id="yndxianCheck" name="yndxianCheck"/>一级运营合伙人
										</div>
									</td>
								</tr>
								<tr>
									<td >是否需要担保：</td>
									<td>
										<select data-am-selected="{searchBox: 1}"  name="yndEnableDanbao" id="yndEnableDanbao" >
											<option value="">请选择</option>
											<option value="1" th:selected="${ (yndProduct == null) ?false: (yndProduct.enable_danbao  == 1)}">需要担保</option>
											<option value="0" th:selected="${ (yndProduct == null) ?false: (yndProduct.enable_danbao  == 0)}">不需要担保</option>
										</select>
									</td>
									<td >可借款月数：</td>
									<td>
										<input  type="text" th:value="${ (yndProduct == null) ?'':yndProduct.loan_months }" id="yndLoanMonths" name="yndLoanMonths"/>
									</td>
									<td colspan="2">多个月份用逗号隔开，如1,3,6,12</td>
								</tr>
								<tr>
									<td colspan="6" >快易贷</td>
								</tr>
								<tr>
									<td >状态：</td>
									<td>
										<select data-am-selected="{searchBox: 1}"  name="kydStatus" id="kydStatus">
											<option th:each="status : ${switchEnum}"  th:value="${status.name()}" th:text="${status.getKgdesc()}" 
											th:selected="${status.name() == ( kydProdcut != null ? kydProdcut.status:'no')}"></option>
										</select> 
									</td>
									<td >最短期限：</td>
									<td >
										<select data-am-selected="{searchBox: 1}"  name="kydMinMonths" id="kydMinMonths">
											<span th:each="min : ${NumEnum}" >
												<span th:if="${min.getNum()} != 0">
													<option th:value="${min.getNum()}" th:text="${min.getNum()} + '个月'" 
													th:selected="${min.getNum() == ( kydProdcut != null? kydProdcut.min_months:1)}" ></option>
												</span>
											</span>
										</select> 
									</td>
									<td>提前还款最短时间：</td>
									<td>
										<select data-am-selected="{searchBox: 1}" name="kydAdvanceMonth" id="kydAdvanceMonth">
											<option th:each="min : ${NumEnum}" th:value="${min.getNum()}" th:text="${min.getNum()} + '个月'" 
											th:selected="${min.getNum() == ( kydProdcut != null? kydProdcut.advance_month:0)}" ></option>
										</select> 
									</td>
								</tr>
								<tr>
									<td rowspan="2">还款方式&amp;利率：</td>
									
									<td ><input type="checkbox" value="debx"  th:checked="${kyddebx != null}" id="kyddebxCheck" name="kyddebxCheck"/>等额本息</td>
									<td ><input type="checkbox" value="dqbx"  th:checked="${kyddqbx != null}" id="kyddqbxCheck" name="kyddqbxCheck"/>到期还本付息</td>
									<td ><input type="checkbox" value="ayfx"  th:checked="${kydayfx != null}" id="kydayfxCheck" name="kydayfxCheck"/>按月付息到期还本</td>
									<td  colspan="2"><input  type="checkbox" value="debj"  th:checked="${kyddebj != null}" id="kyddebjCheck" name="kyddebjCheck"/>等本等息</td>
								</tr>
								<tr>
									<td>日利率<input type="number" min="0" th:value="${ kyddebx != null ? kyddebx :0 }"  id="kyddebxRate" name="kyddebxRate"/>%</td>
									<td>月利率<input min="0" type="number" th:value="${ kyddqbx  != null ? kyddqbx :0}" id="kyddqbxRate" name="kyddqbxRate"/>%</td>
									<td>日利率<input type="number" min="0" th:value="${ kydayfx != null? kydayfx :0}" id="kydayfxRate" name="kydayfxRate"/>%</td>
									<td  colspan="4">日利率<input type="number" min="0" th:value="${ kyddebj != null? kyddebj :0}" id="kyddebjRate" name="kyddebjRate"/>%</td>
								</tr>
								<tr>
									<td >放款合伙人确认 状态：</td>
									<td>
										<select data-am-selected="{searchBox: 1}"  name="kydEnableConfirm" id="kydEnableConfirm" onChange="kydChangeEnableConfirm()">
											<option th:each="status : ${switchEnum}"  th:value="${status.getCode()}" th:text="${status.getKgdesc()}" 
											th:selected="${status.getCode() == ( kydEnableConfirm != null ? kydEnableConfirm :0)}"></option>
										</select>
									</td>
									<td>确认角色：</td>
									<td colspan="3">
										<div id="kydCheckBox">
											<input type="checkbox" value="10"  th:checked="${kydcunCheck != null}"  id="kydcunCheck" name="kydcunCheck"/>市场合伙人  
											<input type="checkbox" value="11"  th:checked="${kydzhenCheck != null}" id="kydzhenCheck" name="kydzhenCheck"/>二级运营合伙人 		       
											<input type="checkbox" value="12"  th:checked="${kydxianCheck != null}" id="kydxianCheck" name="kydxianCheck"/>一级运营合伙人
										</div>
									</td>
								</tr>
								<tr>
									<td >是否需要担保：</td>
									<td>
										<select data-am-selected="{searchBox: 1}"  name="kydEnableDanbao" id="kydEnableDanbao" >
											<option value="">请选择</option>
											<option value="1" th:selected="${ (kydProdcut == null) ? false:kydProdcut.enable_danbao  == 1}">需要担保</option>
											<option value="0" th:selected="${ (kydProdcut == null) ? fasle:kydProdcut.enable_danbao  == 0}">不需要担保</option>
										</select>
									</td>
									<td >可借款月数：</td>
									<td>
										<input  type="text" th:value="${ (kydProdcut == null) ?'':kydProdcut.loan_months  }"  id="kydLoanMonths" name="kydLoanMonths"/>
									</td>
									<td colspan="2">多个月份用逗号隔开，如1,3,6,12</td>
								</tr>
						</tbody>
						</table>						
						<table class="am-table am-table-bordered">
							<tbody>
								<tr><td colspan="6"  class="am-primary">授信额度上限配置：</td></tr>
								<tr>
									<td >授信额度上限：</td>
									<td>
										<input type="number" th:value="${maxCredit!=null ? maxCredit:50000}" id="maxCredit" name="maxCredit" min="0" required="required"/>
									</td>
									<td>借款人借款上限：</td>
									<td><input type="number" th:value="${maxLoan!=null ? maxLoan:50000}" id="maxLoan" name="maxLoan" min="0" required="required"/></td>
									<td>是否同步下级</td>
									<td>
										<select data-am-selected="{searchBox: 1}"  name="isChangeBranch"  id="isChangeBranch">
											<option th:value="false" selected="selected">否</option>
											<option th:value="true">是</option>
										</select>
									</td>
								</tr>
						</tbody>
						</table>
						<table class="am-table am-table-bordered">
							<tbody>
								<tr><td colspan="4"  class="am-primary">产品可用金额配配置：</td></tr>
								<tr>
									<td >优农贷：</td>
									<td><input type="number" th:value="${ yndProduct != null? yndProduct.lend_rate :0}" name="yndLandRate" id="yndLandRate"
									min="0" max="100" required="required"/>%</td>
									<td>快易贷：</td>
									<td><input type="number" th:value="${ kydProdcut != null? kydProdcut.lend_rate :100}" name="kydLandRate" id="kydLandRate"
									 min="0" max="100" required="required"/>%</td>
								</tr>
							</tbody>
						</table>
						
						<div class="am-form-group">
						<div class="am-u-sm-12 am-u-sm-push-5">
							<button type="button" onclick="javascript:$('#sxRuleForm').submit();" class="am-btn am-btn-primary tpl-btn-bg-color-success ">提交</button>
							<button type="button" onclick="loadPage('agentManage/sxRuleUI')" class="am-btn am-btn-success tpl-btn-bg-color-success ">返回</button>
						</div>
					</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
<script th:inline="javascript">
	<![CDATA[
		
	var canSubmit = true;//表单重复提交控制 
	var curVilidate = true;
	var yndStatus= $("#yndStatus").val();//产品启用时必须有一个还款方式
	var kydStatus= $("#kydStatus").val();
	var yndHasCheck = false;
	var kyddHasCheck = false;
	var yndEnableAndNoRole = false;
	var kydEnableAndNoRole = false;
	kydChangeEnableConfirm();
	yndChangeEnableConfirm();
	validateUserForm();
	function validateUserForm(){    
	    $('#sxRuleForm').validate({
	        errorElement : 'span',
	        errorClass : 'am-icon-times',
	        focusInvalid : true,
	        onkeyup:false,
	        ignore : "",
	        rules : {
	        },
	        messages : {
	        },
	        submitHandler : function(form) {
	        	checkSelectHasVal();
	        	yndCheckIsEnableAndNotRole();
	        	kydCheckIsEnableAndNotRole();
	        	if(!curVilidate){
        		    layer.msg("还款方式勾选后对应利率不能为空", {
			               icon : 0
			        });
			        return;
        		}
        		if("yes"==yndStatus && !yndHasCheck){
        		    layer.msg("优农贷已启用,须至少选择还款方式", {
			               icon : 0
			        });
			        return;
        		}
        		if("yes"==kydStatus && !kyddHasCheck){
        		    layer.msg("快易贷已启用,须至少选择还款方式", {
			               icon : 0
			        });
			        return;
        		}
        		if(yndEnableAndNoRole){
        			layer.msg("已启用优农贷放款确认，请选择角色", {
			               icon : 0
			        });
			        return;
        		}
        		debugger;
        		var reg = /^([1-9]+\d*,)*[1-9]+\d*$/;
        		var yndLoanMonths= $("#yndLoanMonths").val();
				var kydLoanMonths= $("#kydLoanMonths").val();
        		if(kydLoanMonths && !reg.test(kydLoanMonths)){
        		    layer.msg("快易贷的可借款月数格式错误", {
			               icon : 0
			        });
			        return;
        		}
        		if(yndLoanMonths && !reg.test(yndLoanMonths)){
        		    layer.msg("优农贷的可借款月数格式错误", {
			               icon : 0
			        });
			        return;
        		}
        		if(kydEnableAndNoRole){
        			layer.msg("已启用快易贷放款确认，请选择角色", {
			               icon : 0
			        });
			        return;
        		}
        		if(canSubmit){
        			canSubmit = false;
        			commit('sxRuleForm', 'agentManage/addOrEditRule','agentManage/sxRuleUI');
        		}
        		
	        }
	    });
	}
	function checkSelectHasVal(){
		yndStatus= $("#yndStatus").val();//产品启用时必须有一个还款方式
		kydStatus= $("#kydStatus").val();
		
		curVilidate = true;
		eachCheck("ynd","debx");
		eachCheck("ynd","debj");
		eachCheck("ynd","ayfx");
		eachCheck("ynd","dqbx");
		eachCheck("kyd","debx");
		eachCheck("kyd","debj");
		eachCheck("kyd","ayfx");
		eachCheck("kyd","dqbx");
	}
	function eachCheck(product,type){
		var check = product +type+"Check";
		var rate = product +type+"Rate";
		var isCheck = $("#" + check).is(':checked');
		var rate = $("#" + rate).val();
		
		if(isCheck && "ynd"==product){
			yndHasCheck = true;
		}
		if(isCheck && "kyd"==product){
			kyddHasCheck = true;
		}
		if(isCheck && rate ==''){
			curVilidate = false;
		}
	}
	function yndCheckIsEnableAndNotRole(){
		var yndEnableConfirm = $('#yndEnableConfirm').val();
		var yndcunCheck =  $("#yndcunCheck").is(':checked');
		var yndzhenCheck = $("#yndzhenCheck").is(':checked');
		var yndxianCheck = $("#yndxianCheck").is(':checked');
		if(yndEnableConfirm == 1){
			if(!yndcunCheck&&!yndzhenCheck&&!yndxianCheck){
				yndEnableAndNoRole = true;
				return;
			}
		}
		yndEnableAndNoRole = false;
	}
	
	function kydCheckIsEnableAndNotRole(){
		var kydEnableConfirm = $('#kydEnableConfirm').val();
		var kydcunCheck =  $("#kydcunCheck").is(':checked');
		var kydzhenCheck = $("#kydzhenCheck").is(':checked');
		var kydxianCheck = $("#kydxianCheck").is(':checked');
		if(kydEnableConfirm == 1){
			if(!kydcunCheck&&!kydzhenCheck&&!kydxianCheck){
				kydEnableAndNoRole = true;
				return;
			}
		}
		kydEnableAndNoRole = false;
	}
	
	function kydChangeEnableConfirm(){
		var enableConfirm = $('#kydEnableConfirm').val();
		if(enableConfirm == 0){
			$('#kydCheckBox').hide();
		}else{
			$("#kydCheckBox").show();
		}
	}
	function yndChangeEnableConfirm(){
		var enableConfirm = $('#yndEnableConfirm').val();
		if(enableConfirm == 0){
			$("#yndCheckBox").hide();
		}else{
			$("#yndCheckBox").show();
		}
	}
	
		]]>
	</script>